package com.wzm.moviepic;


import java.util.ArrayList;

import com.wzm.bean.DownInfo;
import com.wzm.manager.Tag;
import com.wzm.utils.Logger;
import com.wzm.utils.NetworkUtils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;

public class BaseBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
    	//获取网络类型
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            AppApplication.mNetWorkState = NetworkUtils.getNetworkState(context);
            
            Logger.info("捕获到网络状态改变"+AppApplication.mNetWorkState);
            
            if(AppApplication.mNetWorkState == NetworkUtils.NETWORN_WIFI)
            {//启动服务
            	ArrayList<DownInfo> dList = AppApplication.getInstance().getDownList();
            	int curMoiveid = AppApplication.getInstance().getCurDownMovie();
				if(dList.size()>0)
				{//启动下载
					
					Logger.info("网络变成WIFI，启动下载");
					//修复越界
					if(AppApplication.getInstance().getCurDownid()<dList.size())
					{
						Intent service = new Intent(Tag.START);
						service.putExtra("action", Tag.DownPicList);
						service.putExtra("movieid", dList.get(AppApplication.getInstance().getCurDownid()).getMovieid());
						//AppApplication.getInstance().setCurDownMovie(Integer.valueOf(dList.get(AppApplication.getInstance().getCurDownid()).getMovieid()));
						context.startService(service);
					}
					
				}
            }else if(AppApplication.mNetWorkState == NetworkUtils.NETWORN_NONE)
            {
            	ArrayList<DownInfo> dList = AppApplication.getInstance().getDownList();
            	int curMoiveid = AppApplication.getInstance().getCurDownMovie();
				if(dList.size()>0)
				{//启动下载
	            	
	            	if(AppApplication.getInstance().getCurDownid()<dList.size())
					{
	            		Intent service = new Intent(Tag.START);
		            	service.putExtra("action", Tag.DownStop);
	            		service.putExtra("movieid", dList.get(AppApplication.getInstance().getCurDownid()).getMovieid());
	            		context.startService(service);
					}
				}
            }
        }

    }

}
